package controler;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter; 

import model.LoginModel;

import dao.GetCnn;
import dao.common;

/**
 * Servlet implementation class login
 */
@WebServlet("/login")
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @throws IOException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

    protected void processRequest(HttpServletRequest request, HttpServletResponse response){
    	response.setContentType("text/html;charset=UTF-8");
    	String submitName = request.getParameter("action");

		if (submitName.equals("Login")) {

			try {
				String userName = request.getParameter("userName");
				String passWord = request.getParameter("passWord");

				LoginModel login = new LoginModel();

				boolean resuilt = login.checkLogin(userName, passWord);

				String url = "fail.jsp";
		
				
				if (resuilt) {
					if(LoginModel.s ==1){
						HttpSession ss = request.getSession();
						ss.setAttribute("userName", userName);
						url = "trangchu";
					}
					if(LoginModel.s ==2){
						HttpSession ss = request.getSession();
						ss.setAttribute("userName", userName);
						url = "admindanhmuc";
					}
				}

				RequestDispatcher requestDis = request
						.getRequestDispatcher(url);
				requestDis.forward(request, response);

			} catch (Exception e) {
				// TODO: handle exception
			}
		}
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
